package com.ly.lw.styledo.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.view.animation.AlphaAnimation;

import com.alibaba.fastjson.JSONObject;
import com.etsy.android.grid.StaggeredGridView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.ly.lw.styledo.R;
import com.ly.lw.styledo.adapter.MainDetailAdapter;
import com.ly.lw.styledo.bean.Home;
import com.ly.lw.styledo.bean.Normal;
import com.ly.lw.styledo.bean.PlanList;
import com.ly.lw.styledo.bean.ResultJson;
import com.ly.lw.styledo.config.UrlConstants;
import com.ly.lw.styledo.utils.FileUtils;
import com.umeng.message.PushAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sks on 15-7-7.
 */
public class MainDetail extends FragmentActivity{



    private StaggeredGridView staggeredGridView;

    private BitmapUtils bitmapUtils;
    private MainDetailAdapter mainDetailAdapter;
    private ArrayList<Normal> listNormals = new ArrayList<>();
    private ArrayList<PlanList> planLists;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main_detail);
        //ViewUtils.inject(this);

        initBitmap();
        initView();
        loadData();
    }


    private void loadData() {
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.POST, UrlConstants.HOME_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                //获取服务器返回的json数据
                String result = responseInfo.result;
                Home home = JSONObject.parseObject(result, Home.class);
                if (home != null) {

                    ResultJson resultJson = home.getResult();

                    if (resultJson != null) {
                        List<Normal> normals = resultJson.getNormals();
                        if (normals != null && normals.size() > 0) {
                            listNormals.addAll(normals);
                            mainDetailAdapter.notifyDataSetChanged();
                        }


                    }


                }


            }

            @Override
            public void onFailure(HttpException e, String s) {
                s = s+1;

            }
        });
    }

    private void initView() {
        staggeredGridView = (StaggeredGridView) findViewById(R.id.detail_grid_view);
       // staggeredGridView.setFocusable(false);

        mainDetailAdapter = new MainDetailAdapter(planLists, this, bitmapUtils);
        View headView = createHeadView();
        //staggeredGridView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true));
       // staggeredGridView.addHeaderView(headView);
        staggeredGridView.setAdapter(mainDetailAdapter);

    }

    private View createHeadView() {
        return null;
    }

    /**
     * shop_no=10000034
     appVersion=1.4.6
     os=android
     osVersion=4.1.2

     */
    private RequestParams getRequestParams() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("appVersion", "1.4.6");
        //params.addBodyParameter("shop_grp_cd","10" );
        params.addBodyParameter("shop_no", "10000034");
        params.addBodyParameter("os", "android");
        // params.addBodyParameter("osVersion", "");

        return params;
    }

    private void initBitmap() {
        bitmapUtils = new BitmapUtils(this, FileUtils.getImageCache());
        //bitmapUtils.configDefaultLoadFailedImage(R.mipmap.ic_default_item);
        // bitmapUtils.configDefaultLoadingImage(R.color.white);

        AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(1000);
        bitmapUtils.configDefaultImageLoadAnimation(animation);
        bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);//设置图片压缩类型

        bitmapUtils.configMemoryCacheEnabled(true);
        bitmapUtils.configDiskCacheEnabled(true);
    }
}
